package com.yitao.cms.controller.activity;


import com.google.common.collect.Lists;
import com.skipper.base.bean.DataOutput;
import com.skipper.base.bean.PagedInput;
import com.skipper.base.bean.PagedOutput;
import com.yitao.client.constant.ActivityEnums;
import com.yitao.cms.service.activity.CmsProductGroupActivityService;
import com.yitao.cms.utils.BaseUtils;
import com.yitao.cms.utils.DateUtils;
import com.yitao.cms.vo.CategoryVo;
import com.yitao.cms.vo.activity.ProductGroupActivityInfoVo;
import com.yitao.cms.vo.activity.ProductGroupActivityVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/cms/activity", produces = {"application/json"})
@Api(description = "运营后台活动相关接口", tags = {"cms-activity"})
public class CmsProductGroupActivityController {

    private BaseUtils base = BaseUtils.getInstance();

    @Autowired
    private CmsProductGroupActivityService productGroupActivityService;

    @ApiOperation("活动列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public PagedOutput<ProductGroupActivityVo> getProductGroupActivityModelList(
            @ApiParam(value = "产品名称") @RequestParam(value = "productName", required = false) String productName,
            @ApiParam(value = "产品Code") @RequestParam(value = "productCode", required = false) String productCode,
            @ApiParam(value = "活动状态") @RequestParam(value = "activityStates", required = false) Integer activityStates,
            @ApiParam(value = "开始时间") @RequestParam(value = "staDate", required = false) String staDate,
            @ApiParam(value = "结束时间") @RequestParam(value = "endDate", required = false) String endDate,
            @ApiParam(value = "分页参数，表示当前页", required = true, defaultValue = "1") @RequestParam(value = "pageNo") Integer pageNo,
            @ApiParam(value = "分页参数，表示每页显示多少条记录", required = true, defaultValue = "10") @RequestParam(value = "pageSize") Integer pageSize,
            HttpServletRequest request) {
        return productGroupActivityService.getProductGroupActivityList(new PagedInput(pageSize, pageNo), productName, productCode, staDate, endDate, activityStates);
    }

    @ApiOperation("活动详情")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public DataOutput<ProductGroupActivityInfoVo> getProductGroupActivityById(@ApiParam(value = "活动ID") @RequestParam(value = "activity", required = false) Long activityId) {
        return productGroupActivityService.getProductGroupActivityById(activityId);
    }


    @ApiOperation("活动添加或者修改")
    @RequestMapping(value = "/updateOrAdd", method = RequestMethod.POST)
    public DataOutput addOrUpdateProductGroupActivity(HttpServletRequest request, @RequestBody ProductGroupActivityInfoVo productGroupActivityInfoVo) {
        if (productGroupActivityInfoVo.getProductId() == null) {
            return base.getErrorMsg();
        }
        if (!StringUtils.isEmpty(productGroupActivityInfoVo.getStartTime())) {
            productGroupActivityInfoVo.setStartTime(productGroupActivityInfoVo.getStartTime() + " 00:00:00");
        }
        if (!StringUtils.isEmpty(productGroupActivityInfoVo.getEndTime())) {
            productGroupActivityInfoVo.setEndTime(productGroupActivityInfoVo.getEndTime() + " 23:59:59");
        }
        return productGroupActivityService.addOrUpdateProductGroupActivity(request, productGroupActivityInfoVo);
    }

    @ApiOperation("活动修改状态")
    @RequestMapping(value = "/updateStates", method = RequestMethod.POST)
    public DataOutput updateProductGroupActivityActivityStates(HttpServletRequest request,
                                                               @ApiParam(value = "活动ID") @RequestParam(value = "activityId", required = false) Long activityId,
                                                               @ApiParam(value = "活动状态") @RequestParam(value = "activityStates", required = false) Integer activityStates) {
        return productGroupActivityService.updateProductGroupActivityActivityStates(activityId, activityStates, request);
    }


    @ApiOperation("删除活动")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public DataOutput deleteProductGroupActivity(HttpServletRequest request,
                                                 @ApiParam(value = "活动ID") @RequestParam(value = "activityId", required = false) Long activityId) {
        return productGroupActivityService.deleteProductGroupActivityById(activityId, request);
    }

    @ApiOperation("状态枚举 未发布 1 已经发布 1 已经结束 2")
    @RequestMapping(value = "/activityStatesEnum", method = RequestMethod.POST)
    public DataOutput getActivityStates() {
        ActivityEnums.ActivityStatesEnum[] values = ActivityEnums.ActivityStatesEnum.values();
        List<CategoryVo> categoryVoList = Lists.newArrayList();
        CategoryVo categoryVo = null;
        for (ActivityEnums.ActivityStatesEnum activityStatesEnum : values) {
            categoryVo = new CategoryVo();
            categoryVo.setKey(activityStatesEnum.getCode());
            categoryVo.setValue(activityStatesEnum.getName());
            categoryVoList.add(categoryVo);
        }
        return base.getSuccessMsg(categoryVoList);
    }
}
